package com.situ.staff.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.situ.staff.bean.User;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	// 处理器完成之后 执行3
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	// 处理器执行之后来执行
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	// 处理器执行前来执行
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {

		HttpSession session = request.getSession();

		User attribute = (User) session.getAttribute("user");

		if (attribute != null) {
			return true;
		} else {
			request.setAttribute("msg", "请先登录");
			request.getRequestDispatcher("/user/login").forward(request, response);
			/* response.sendRedirect(request.getContextPath() + "/user/login"); */
			return false;
		}

	}

}
